package zw_901_1000.zw_989_数组形式的整数加法;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class Solution {
    public static void main(String[] args) {
        int[] num = {2, 1, 5};
        int k = 806;
        List<Integer> list = addToArrayForm(num, k);
        list.forEach(System.out::print);
    }

    public static List<Integer> addToArrayForm(int[] A, int K) {
        int n = A.length;
        LinkedList<Integer> res = new LinkedList<>();
        int sum = 0, carry = 0;
        for (int i = n - 1; i >= 0 || K != 0; K = K / 10, i--) {  // 循环条件：两个数有一个没完
            int x = i >= 0 ? A[i] : 0;
            int y = K != 0 ? K % 10 : 0;

            sum = x + y + carry;
            carry = sum / 10;
            res.addFirst(sum % 10);
        }
        if (carry != 0) res.add(0, carry);
        return res;
    }
}
